package faf.business.tests;

import java.util.HashMap;

import javax.persistence.EntityManager;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import faf.business.PropertiesManager;
import faf.dao.JpaUtils;
import faf.entities.Properties;

public class PropertiesManagerTests {

	static JpaUtils jpautils;
	static PropertiesManager manager;

	@BeforeClass
	public static void beforeClass() {
		HashMap<String, String> config = new HashMap<String, String>();
		config.put("hibernate.hbm2ddl.auto", "create-drop");
		jpautils = new JpaUtils(config);
		manager = new PropertiesManager();
		manager.setJpautils(jpautils);
	}
	
	@Test
	public void testCreateProperties() {
		Properties property = new Properties();
		
		manager.createProperties(property);
		
		Assert.assertNotNull(property.getId());
		
		EntityManager em = jpautils.createEntityManger();
		em.getTransaction().begin();
		property = em.merge(property);
		em.getTransaction().commit();
		em.close();
	}
	
	@Test
	public void testDeleteProperties() {
		Properties property = new Properties();
		
		EntityManager em = jpautils.createEntityManger();
		em.getTransaction().begin();
		em.persist(property);
		em.getTransaction().commit();
		em.close();
		
		manager.deleteProperties(property.getId());
		
		em = jpautils.createEntityManger();
		property = em.find(Properties.class, property.getId());
		Assert.assertNull(property);
		em.close();
	}
}
